\section{Code for problem 5}
\label{code5}
\small
\begin{verbatim}
# This function generates an estimate of the mean of the double exponential distribution using constrol variates.

variates <- function(N)
{
  res <- c()
  X <- c()
  Y <- c()
  for(n in 1:N)
  {
    u <- runif(1)
    y <- -log(-log(u))
    x <- -log(1-u)
    
    X <- c(X,x)
    Y <- c(Y,y)
  }
  
  res$X <- X
  res$X_mean <- mean(X)
  res$Y <- Y
  res$Y_mean <- mean(Y)
  
  return(res)
}
doubleExponentialMeanControlVariates <- function(N, M)
{
  temp <- variates(M)
  b <- sqrt(var(temp$Y))/sqrt(var(temp$X)) * cor(temp$X, temp$Y)
  
  temp <- variates(N)
  Y_b <- temp$Y - b * (temp$X - 1)
  std_Y_b <- sqrt (var(Y_b))
  err <- 1.96 * std_Y_b / sqrt(N)
  Y_b <- mean (Y_b)
  
  res <- c()
  # Results obtained using control variates.
  res$Y_b_CV <- Y_b
  res$Y_b_Error_CV <- err
  
  # Results obtained using through standard application of inverse transform theorem.
  res$Y_NoControl <- mean(temp$Y)
  res$Y_Error_NoControl <- 1.96 * sqrt (var(temp$Y)) / sqrt(N)

  return (res)
}
res <- doubleExponentialMeanControlVariates(100, 10000)
\end{verbatim}
\newpage